package com.ruoyi.web.controller.api;


import com.ruoyi.cat.domain.CatActivityTable;
import com.ruoyi.cat.domain.CatBannerTable;
import com.ruoyi.cat.domain.DTO.ArticletaskDTO;
import com.ruoyi.cat.domain.DTO.AwesomeTeammateDTO;
import com.ruoyi.cat.domain.DTO.DataScreeningDTO;
import com.ruoyi.cat.domain.VO.CatForward;
import com.ruoyi.cat.domain.VO.ChartVO;
import com.ruoyi.cat.domain.VO.DataScreeningVO;
import com.ruoyi.cat.service.ICatActivityTableService;
import com.ruoyi.cat.service.ICatArticletaskTableService;
import com.ruoyi.cat.service.ICatIprecordTableService;
import com.ruoyi.cat.service.ICatTradedetailTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.WxTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Api(tags = "api-数据分析")
@RestController
@RequestMapping("/api/dataAnalysis")
@Slf4j
public class DataAnalysisController extends BaseController {

    @Autowired
    private WxTokenService wxTokenService;

    @Autowired
    private ICatIprecordTableService catIprecordTableService;


    @Autowired
    private ICatTradedetailTableService iCatTradedetailTableService;

    @Autowired
    private ICatActivityTableService iCatActivityTableService;

    @Autowired
    private ICatArticletaskTableService iCatArticletaskTableService;

    @ApiOperation("api-活动数据总览")
    @GetMapping("/activityDataScreening")
    public AjaxResult activityDataScreening (HttpServletRequest request, DataScreeningVO dataquery)
    {
        log.info("活动数据总览 :{}",dataquery);
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        //WxLoginUser wxLoginUser = new WxLoginUser();
        //wxLoginUser.setUserId(127L);
        dataquery.setUserId(wxLoginUser.getUserId());
        if (dataquery.getType().equals("1")){
            ArticletaskDTO articletaskDTO = iCatArticletaskTableService.selectCatArticletaskById(dataquery.getActivityId());
            DataScreeningDTO dataScreeningDTO = new DataScreeningDTO();
            Long sharingVisitorCount = catIprecordTableService.getIprecordTableDay(dataquery);
            dataScreeningDTO.setSharingVisitorCount(sharingVisitorCount);
            dataquery.setUserId(wxLoginUser.getUserId());
            dataquery.setAreaValue(articletaskDTO.getCity());
            Long table = catIprecordTableService.getIprecordTableByNo(dataquery);
            dataScreeningDTO.setUniqueVisitor(table);
            if (articletaskDTO.getCity() != null && articletaskDTO.getProvince() != null) {
                dataScreeningDTO.setPlatformTasks(articletaskDTO.getProvince() + articletaskDTO.getCity());
            }

            log.info("dataquery:{}",dataquery);
            AwesomeTeammateDTO awesomeTeammateDTO = iCatTradedetailTableService.getUserIdData(dataquery);
            log.info("awesomeTeammateDTO:{}",awesomeTeammateDTO);
            dataquery.setUserId(wxLoginUser.getUserId());
            AwesomeTeammateDTO userIdTest = iCatTradedetailTableService.getUserIdTest(dataquery);
            dataScreeningDTO.setUpdateDate(new Date());
            if (awesomeTeammateDTO != null) {
                log.info("awesomeTeammateDTO:{}",awesomeTeammateDTO);
                dataScreeningDTO.setUpdateDate(awesomeTeammateDTO.getCreateTime());
                if (awesomeTeammateDTO.getHelpingPeople() != null){
                    dataScreeningDTO.setNumberVisitors(awesomeTeammateDTO.getHelpingPeople());
                }
                if (awesomeTeammateDTO.getTradeNum() != null){
                    dataScreeningDTO.setTotalRevenue(awesomeTeammateDTO.getTradeNum());
                }
            }else {
                dataScreeningDTO.setNumberVisitors(0L);
            }
            if (userIdTest != null) {
                if (userIdTest.getTradeNum().compareTo(BigDecimal.ZERO) != 0) {
                    dataScreeningDTO.setPlatformRewards(userIdTest.getTradeNum());
                }else{
                    dataScreeningDTO.setPlatformRewards(BigDecimal.ZERO);
                }
            }
            // 判断 awesomeTeammateDTO 和 getHelpingPeople 是否为空
            if (awesomeTeammateDTO != null && awesomeTeammateDTO.getHelpingPeople() != null) {
                sharingVisitorCount += awesomeTeammateDTO.getHelpingPeople();
            }
            dataScreeningDTO.setActivityAddress(articletaskDTO.getActivityAddress());
            dataScreeningDTO.setTotalVisitors(sharingVisitorCount);
            return success(dataScreeningDTO);
        }else{
            dataquery.setType("2");
            CatActivityTable articletaskDTO = iCatActivityTableService.selectCatActivityTableByIdTask(dataquery.getActivityId());
           /* if (articletaskDTO.getCity() != null) {
                dataquery.setAreaValue(articletaskDTO.getCity());
            }*/
            DataScreeningDTO dataScreeningDTO = new DataScreeningDTO();
            CatForward catForward =  new CatForward();
            catForward.setUserId(dataquery.getUserId());
            catForward.setArticleId(dataquery.getActivityId());
            Long sharingVisitorCount =  catIprecordTableService.getIprecordTableDay(dataquery);
            //CatForward catForward1 = catIprecordTableService.selectArticleByUserId(catForward);
            dataScreeningDTO.setSharingVisitorCount(sharingVisitorCount);
            dataquery.setAreaValue(articletaskDTO.getCity());
            dataScreeningDTO.setUniqueVisitor(catIprecordTableService.getIprecordTableByNo(dataquery));
            dataquery.setAreaValue(null);
            if (articletaskDTO.getCity() != null && articletaskDTO.getProvince() != null) {
                dataScreeningDTO.setPlatformTasks(articletaskDTO.getProvince() + articletaskDTO.getCity());
            }
            dataScreeningDTO.setUpdateDate(new Date());
            dataquery.setUserId(wxLoginUser.getUserId());
            AwesomeTeammateDTO awesomeTeammateDTO = iCatTradedetailTableService.getUserIdData(dataquery);
            AwesomeTeammateDTO userIdTest = iCatTradedetailTableService.getUserIdTest(dataquery);
            if (awesomeTeammateDTO != null) {
                if (awesomeTeammateDTO.getHelpingPeople() != null){
                    dataScreeningDTO.setNumberVisitors(awesomeTeammateDTO.getHelpingPeople());
                    sharingVisitorCount += awesomeTeammateDTO.getHelpingPeople();
                }
                if (awesomeTeammateDTO.getTradeNum() != null){
                    dataScreeningDTO.setTotalRevenue(awesomeTeammateDTO.getTradeNum());
                }
                List<ChartVO> voList = catIprecordTableService.getActivityData(dataquery);
                log.info("voList:{}",voList);
                if (voList != null && voList.size() > 0) {
                    dataScreeningDTO.setUpdateDate(voList.get(0).getRecordDate());
                }
            }else {
                dataScreeningDTO.setNumberVisitors(0L);
            }
            dataScreeningDTO.setTotalVisitors(sharingVisitorCount);
            dataScreeningDTO.setActivityAddress(articletaskDTO.getActivityAddress());
            return success(dataScreeningDTO);
        }

    }



    @ApiOperation("api-文章数据总览")
    @GetMapping("/dataScreening")
    public AjaxResult dataScreening (HttpServletRequest request, DataScreeningVO dataquery)
    {
        dataquery.setType("1");
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        ArticletaskDTO articletaskDTO = iCatArticletaskTableService.selectCatArticletaskById(dataquery.getActivityId());
        DataScreeningDTO dataScreeningDTO = new DataScreeningDTO();
        dataquery.setUserId(wxLoginUser.getUserId());
        Long sharingVisitorCount = catIprecordTableService.getIprecordTableDay(dataquery);
        dataScreeningDTO.setSharingVisitorCount(sharingVisitorCount);
        dataquery.setUserId(wxLoginUser.getUserId());
        dataquery.setAreaValue(articletaskDTO.getCity());
        Long table = catIprecordTableService.getIprecordTableByNo(dataquery);
        dataScreeningDTO.setUniqueVisitor(table);
        if (articletaskDTO.getCity() != null && articletaskDTO.getProvince() != null) {
            dataScreeningDTO.setPlatformTasks(articletaskDTO.getProvince() + articletaskDTO.getCity());
        }

        log.info("dataquery:{}",dataquery);
        AwesomeTeammateDTO awesomeTeammateDTO = iCatTradedetailTableService.getUserIdData(dataquery);
        log.info("awesomeTeammateDTO:{}",awesomeTeammateDTO);
        dataquery.setUserId(wxLoginUser.getUserId());
        AwesomeTeammateDTO userIdTest = iCatTradedetailTableService.getUserIdTest(dataquery);
        dataScreeningDTO.setUpdateDate(new Date());
        if (awesomeTeammateDTO != null) {
            log.info("awesomeTeammateDTO:{}",awesomeTeammateDTO);
            dataScreeningDTO.setUpdateDate(awesomeTeammateDTO.getCreateTime());
            if (awesomeTeammateDTO.getHelpingPeople() != null){
                dataScreeningDTO.setNumberVisitors(awesomeTeammateDTO.getHelpingPeople());
            }
            if (awesomeTeammateDTO.getTradeNum() != null){
                dataScreeningDTO.setTotalRevenue(awesomeTeammateDTO.getTradeNum());
            }
        }else {
            dataScreeningDTO.setNumberVisitors(0L);
        }
        if (userIdTest != null) {
            if (userIdTest.getTradeNum().compareTo(BigDecimal.ZERO) != 0) {
                dataScreeningDTO.setPlatformRewards(userIdTest.getTradeNum());
            }else{
                dataScreeningDTO.setPlatformRewards(BigDecimal.ZERO);
            }
        }
        // 判断 awesomeTeammateDTO 和 getHelpingPeople 是否为空
        if (awesomeTeammateDTO != null && awesomeTeammateDTO.getHelpingPeople() != null) {
            sharingVisitorCount += awesomeTeammateDTO.getHelpingPeople();
        }
        dataScreeningDTO.setActivityAddress(articletaskDTO.getActivityAddress());
        dataScreeningDTO.setTotalVisitors(sharingVisitorCount);
        return success(dataScreeningDTO);
    }

    @ApiOperation("api-文章最给力的队友")
    @GetMapping("/getAwesomeTeammate")
    public AjaxResult getAwesomeTeammate (HttpServletRequest request,DataScreeningVO dataquery)
    {
        log.info("活动最给力的队友 :{}",dataquery);
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        if (dataquery.getType().equals("1")){
            dataquery.setType("1");
            dataquery.setUserId(wxLoginUser.getUserId());
            return success(iCatTradedetailTableService.getAwesomeTeammate(dataquery));
        }else {
            dataquery.setUserId(wxLoginUser.getUserId());
            dataquery.setType("2");
            dataquery.setUserId(wxLoginUser.getUserId());
            return success(iCatTradedetailTableService.getAwesomeTeammate(dataquery));
        }

    }


    @ApiOperation("api-活动最给力的队友")
    @GetMapping("/getMostValuableTeammate")
    public AjaxResult getMostValuableTeammate (HttpServletRequest request,DataScreeningVO dataquery)
    {
        log.info("活动最给力的队友 :{}",dataquery);
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
      //  WxLoginUser wxLoginUser = new WxLoginUser();
        dataquery.setUserId(wxLoginUser.getUserId());
        dataquery.setType("2");
        dataquery.setUserId(wxLoginUser.getUserId());
        return success(iCatTradedetailTableService.getAwesomeTeammate(dataquery));
    }



    /*@ApiOperation("api- 活动数据总览")
    @GetMapping("/getActivityOverview")
    public AjaxResult getActivityOverview (HttpServletRequest request, DataScreeningVO dataquery)
    {

        CatActivityTable articletaskDTO = iCatActivityTableService.selectCatActivityTableByIdTask(dataquery.getActivityId());
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        if (articletaskDTO == null) {
            return error("无效的活动id");
        }
        dataquery.setType("2");
        //dataquery.setUserId(14L);
        dataquery.setUserId(wxLoginUser.getUserId());
        dataquery.setAreaValue(articletaskDTO.getCity());
        DataScreeningDTO dataScreeningDTO = new DataScreeningDTO();
        dataScreeningDTO.setSharingVisitorCount(catIprecordTableService.getIprecordTableBy(dataquery));
        dataScreeningDTO.setUniqueVisitor(catIprecordTableService.getIprecordTableByNo(dataquery));
        dataScreeningDTO.setPlatformTasks(articletaskDTO.getProvince() + articletaskDTO.getCity());
        AwesomeTeammateDTO awesomeTeammateDTO = iCatTradedetailTableService.getUserIdData(dataquery);
        AwesomeTeammateDTO userIdTest = iCatTradedetailTableService.getUserIdTest(dataquery);
        if (awesomeTeammateDTO != null) {
            dataScreeningDTO.setNumberVisitors(awesomeTeammateDTO.getHelpingPeople());
            dataScreeningDTO.setUpdateDate(awesomeTeammateDTO.getCreateTime());
            if (dataScreeningDTO.getSharingVisitorCount() != null){
                dataScreeningDTO.setTotalVisitors(awesomeTeammateDTO.getHelpingPeople()+dataScreeningDTO.getSharingVisitorCount());
            }else{
                dataScreeningDTO.setTotalVisitors(awesomeTeammateDTO.getHelpingPeople());
            }
        }else {
            dataScreeningDTO.setNumberVisitors(0L);
            dataScreeningDTO.setTotalVisitors(dataScreeningDTO.getSharingVisitorCount());
        }
        //dataScreeningDTO.setPlatformRewards(userIdTest.getTradeNum());
        if (userIdTest != null) {
            if (userIdTest.getTradeNum().compareTo(BigDecimal.ZERO) != 0) {
                dataScreeningDTO.setPlatformRewards(userIdTest.getTradeNum());
            }else{
                dataScreeningDTO.setPlatformRewards(BigDecimal.ZERO);
            }
        }
        return success(dataScreeningDTO);
    }*/

    @ApiOperation("api- 我的任务活动数据总览")
    @GetMapping("/getActivityOverview")
    public AjaxResult getActivityOverview (HttpServletRequest request, DataScreeningVO dataquery)
    {
        log.info("我的任务活动数据总览："+dataquery);
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        dataquery.setUserId(wxLoginUser.getUserId());
        log.info("活动数据总览："+dataquery);
        return success(catIprecordTableService.getActivityOverview(dataquery));
    }


    @ApiOperation("api- 我的任务折线图")
    @GetMapping("/getActivityChart")
    public AjaxResult getActivityChart (HttpServletRequest request, DataScreeningVO dataquery)
    {
        log.info("我的任务折线图："+dataquery);
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        dataquery.setUserId(wxLoginUser.getUserId());
       // dataquery.setType("2");
        return success(catIprecordTableService.getActivityChart(dataquery));
    }


    @ApiOperation("api- 我的活动数据总览")
    @GetMapping("/getArticleOverview")
    public AjaxResult getArticleOverview (HttpServletRequest request, DataScreeningVO dataquery)
    {
        log.info("我的活动数据总览："+dataquery);
       // WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
       // dataquery.setUserId(wxLoginUser.getUserId());
        return success(catIprecordTableService.getArticleOverview(dataquery));
    }


    @ApiOperation("api- 我的活动折线图")
    @GetMapping("/getArticleChart")
    public AjaxResult getArticleChart (HttpServletRequest request, DataScreeningVO dataquery)
    {
        log.info("我的活动："+dataquery);
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
      //  dataquery.setUserId(wxLoginUser.getUserId());
      //  dataquery.setType("2");
        return success(catIprecordTableService.getActivityChart(dataquery));
    }



}
